/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bimager.jtwain.ui;

import com.bimager.config.Config;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import javax.swing.AbstractButton;

/**
 *
 * @author Amskape
 */
public class Preference extends javax.swing.JPanel {

    /**
     * Creates new form Preference
     */
    Properties settings =new Properties();
    public Preference() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pixeltypegroup = new javax.swing.ButtonGroup();
        transfergroup = new javax.swing.ButtonGroup();
        commpressiongroup = new javax.swing.ButtonGroup();
        twainSettingPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        showUI = new javax.swing.JCheckBox();
        pixelcolorradio = new javax.swing.JRadioButton();
        pixelgrayradio = new javax.swing.JRadioButton();
        radionative = new javax.swing.JRadioButton();
        radiobuffer = new javax.swing.JRadioButton();
        jPanel3 = new javax.swing.JPanel();
        radiocompress = new javax.swing.JRadioButton();
        radiouncompress = new javax.swing.JRadioButton();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator1 = new javax.swing.JSeparator();
        ImageSettingPanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        settingSave = new javax.swing.JButton();
        settingOk = new javax.swing.JButton();

        twainSettingPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Twain"));

        jLabel1.setText("Show UI");

        jLabel2.setText("Pixel Type");

        jLabel3.setText("Transfer Type");

        showUI.setSelected(true);

        pixeltypegroup.add(pixelcolorradio);
        pixelcolorradio.setSelected(true);
        pixelcolorradio.setText("Color");

        pixeltypegroup.add(pixelgrayradio);
        pixelgrayradio.setText("Gray");

        transfergroup.add(radionative);
        radionative.setSelected(true);
        radionative.setText("Native");
        radionative.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radionativeActionPerformed(evt);
            }
        });

        transfergroup.add(radiobuffer);
        radiobuffer.setText("Buffered");
        radiobuffer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radiobufferActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Compression"));

        radiocompress.setText("Compressed");
        radiocompress.setEnabled(false);

        radiouncompress.setSelected(true);
        radiouncompress.setText("Uncompressed");
        radiouncompress.setEnabled(false);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(radiocompress)
                .addGap(18, 18, 18)
                .addComponent(radiouncompress))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(radiocompress)
                    .addComponent(radiouncompress))
                .addContainerGap(34, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout twainSettingPanelLayout = new javax.swing.GroupLayout(twainSettingPanel);
        twainSettingPanel.setLayout(twainSettingPanelLayout);
        twainSettingPanelLayout.setHorizontalGroup(
            twainSettingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(twainSettingPanelLayout.createSequentialGroup()
                .addGroup(twainSettingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, twainSettingPanelLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 346, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(twainSettingPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(twainSettingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(twainSettingPanelLayout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, twainSettingPanelLayout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 346, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(twainSettingPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addGap(59, 59, 59)
                        .addGroup(twainSettingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(showUI)
                            .addGroup(twainSettingPanelLayout.createSequentialGroup()
                                .addComponent(pixelcolorradio)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(pixelgrayradio)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(twainSettingPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addGap(31, 31, 31)
                .addGroup(twainSettingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(twainSettingPanelLayout.createSequentialGroup()
                        .addComponent(radionative)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(radiobuffer)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        twainSettingPanelLayout.setVerticalGroup(
            twainSettingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(twainSettingPanelLayout.createSequentialGroup()
                .addGroup(twainSettingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(showUI))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(twainSettingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(pixelcolorradio)
                    .addComponent(pixelgrayradio))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(twainSettingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(radionative)
                    .addComponent(radiobuffer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(52, Short.MAX_VALUE))
        );

        ImageSettingPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Image"));

        javax.swing.GroupLayout ImageSettingPanelLayout = new javax.swing.GroupLayout(ImageSettingPanel);
        ImageSettingPanel.setLayout(ImageSettingPanelLayout);
        ImageSettingPanelLayout.setHorizontalGroup(
            ImageSettingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        ImageSettingPanelLayout.setVerticalGroup(
            ImageSettingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 112, Short.MAX_VALUE)
        );

        settingSave.setText("Save");
        settingSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                settingSaveActionPerformed(evt);
            }
        });

        settingOk.setText("OK");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(settingSave)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(settingOk, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(0, 12, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(settingSave)
                    .addComponent(settingOk)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(twainSettingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(ImageSettingPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(twainSettingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ImageSettingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void radiobufferActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radiobufferActionPerformed
        // TODO add your handling code here:
        radiocompress.setEnabled(true);
        radiouncompress.setEnabled(true);
    }//GEN-LAST:event_radiobufferActionPerformed

    private void radionativeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radionativeActionPerformed
        // TODO add your handling code here:
        radiocompress.setEnabled(false);
        radiouncompress.setEnabled(false);
        radiouncompress.setSelected(true);
    }//GEN-LAST:event_radionativeActionPerformed

    private void settingSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_settingSaveActionPerformed
        // TODO add your handling code here:
        try {
    		//set the properties value
            
               // Config.class.getResourceAsStream("setting")
           String pixeltype = "Color";
           String compression = "Uncompressed";
           Integer showui = 1;
           String transfer = "Native";
           for (Enumeration<AbstractButton> buttons = pixeltypegroup.getElements(); buttons.hasMoreElements();) {
            AbstractButton button = buttons.nextElement();

            if (button.isSelected()) {
                pixeltype=  button.getText();
            }
        }
             for (Enumeration<AbstractButton> buttons =transfergroup.getElements(); buttons.hasMoreElements();) {
            AbstractButton button = buttons.nextElement();

            if (button.isSelected()) {
                transfer=  button.getText();
            }
        }
               for (Enumeration<AbstractButton> buttons = commpressiongroup.getElements(); buttons.hasMoreElements();) {
            AbstractButton button = buttons.nextElement();

            if (button.isSelected()) {
                compression=  button.getText();
            }
        }
           
               if(!showUI.isSelected())
                    showui = 0;
               
    		settings.setProperty("pixeltype",pixeltype);
    		settings.setProperty("compression",compression);
    		settings.setProperty("showui", showui.toString());
                settings.setProperty("transfer",transfer);
                
               // Config.class.
               // FileOutputStream settingout = new FileOutputStream(null);
              //  new FileOutputStream(null)
    		//save properties to project root folder
    		//settings.store(new FileOutputStream("config.properties"), null);
 
    	} catch (Exception ex) {
    		ex.printStackTrace();
        }
        
        
    }//GEN-LAST:event_settingSaveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel ImageSettingPanel;
    private javax.swing.ButtonGroup commpressiongroup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JRadioButton pixelcolorradio;
    private javax.swing.JRadioButton pixelgrayradio;
    private javax.swing.ButtonGroup pixeltypegroup;
    private javax.swing.JRadioButton radiobuffer;
    private javax.swing.JRadioButton radiocompress;
    private javax.swing.JRadioButton radionative;
    private javax.swing.JRadioButton radiouncompress;
    private javax.swing.JButton settingOk;
    private javax.swing.JButton settingSave;
    private javax.swing.JCheckBox showUI;
    private javax.swing.ButtonGroup transfergroup;
    private javax.swing.JPanel twainSettingPanel;
    // End of variables declaration//GEN-END:variables
}
